package com.alibaba.csp.sentinel.dashboard.util;

import org.apache.commons.lang.time.DateFormatUtils;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 用途描述： 生成ID，只会减少碰撞，极端情况下还是会ID重复，集群并发增加碰撞概率集群部署需要各节点时钟同步
 *
 * @author YangLong
 * @version V1.0
 * @since 2024/2/27
 */
public class IdUtils {
    private static AtomicLong ids = new AtomicLong(0);

    public synchronized static long nextId(){
        String now = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSSS");
        long id = Long.parseLong(now + ids.incrementAndGet());
        return id;
    }

    public static void main(String[] args) {
        System.out.println(nextId());
    }
}
